﻿using King.Box.Utils.Global;
using King.Box.Utils.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace King.Box.Utils.Utils
{
    public static class Util
    {
        public static string ParseDictionaryToQueryString(IDictionary<string, object> query)
        {
            StringBuilder builder = new StringBuilder();
            if (query != null)
            {
                foreach (KeyValuePair<string, object> pair in query)
                {
                    string str = null;

                    if (pair.Value is string)
                        str = pair.Value as string;
                    else if (pair.Value is DateTime)
                        str = Convert.ToDateTime(pair.Value).ToString(CultureInfo.InvariantCulture);
                    else
                        str = JsonConvert.SerializeObject(pair.Value);

                    builder.Append(pair.Key + "=" + HttpUtility.UrlEncode(str) + "&");
                }

                if (builder.Length > 0)
                    builder.Remove(builder.Length - 1, 1);
            }
            return builder.ToString();
        }

        public static Dictionary<string, string> ParseQueryStringToDictionary(string queryString)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(queryString))
            {
                string[] kv = queryString.Split('&');
                foreach (string str in kv)
                {
                    var temp = str.Split('=');
                    if (temp.Length < 2)
                        continue;
                    dic.Add(temp[0], temp[1]);
                }
            }
            return dic;
        }

        /// <summary>  
        /// 枚举转字典集合  
        /// </summary>  
        /// <typeparam name="T">枚举类名称</typeparam>  
        /// <param name="keyDefault">默认key值</param>  
        /// <param name="valueDefault">默认value值</param>  
        /// <returns>返回生成的字典集合</returns>  
        public static Dictionary<string, object> ParseEnumToDictionary<T>(string keyDefault = null, int valueDefault = GlobalConst.AllSelectValue)
        {
            Dictionary<string, object> dicEnum = new Dictionary<string, object>();
            Type enumType = typeof(T);
            if (!enumType.IsEnum)
                return dicEnum;

            if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项  
                dicEnum.Add(keyDefault, valueDefault);

            string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组  
            foreach (var item in fieldstrs)
            {
                string description = string.Empty;
                var field = enumType.GetField(item);
                object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组  
                if (arr != null && arr.Length > 0)
                    description = ((DescriptionAttribute)arr[0]).Description;   //属性描述  
                else
                    description = item;  //描述不存在取字段名称  

                dicEnum.Add(description, (int)Enum.Parse(enumType, item));  //不用枚举的value值作为字典key值的原因从枚举例子能看出来，其实这边应该判断他的值不存在，默认取字段名称  
            }
            return dicEnum;
        }
    }
}
